package com.atguigu.gmall.product.cron;

import com.atguigu.gmall.product.service.SkuInfoService;

import com.atguigu.gmall.starter.redisson.SkuBloomTask;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
@Slf4j
@Component
public class SkuIdBloomTask implements SkuBloomTask {



    @Autowired
    SkuInfoService skuInfoService;
    @Autowired
    RBloomFilter<Object> skuBloom;
    @Qualifier("skuIdBloom")
    @Autowired
    RBloomFilter<Object> skuIdBloom;
    //秒 分 时 日 月 周几
    @Scheduled(cron = "0 0 3 * * 3")
    public void rebuildBloom(){
        //重建布隆
        log.info("系统正在重建sku布隆");
        skuIdBloom.delete();
        skuIdBloom.tryInit(5000000, 0.0000001);

        initData(skuIdBloom);
    }



    @Override
    public void initData(RBloomFilter<Object> skuFilter) {
        log.info("布隆过滤器正在重建");
        //去数据库查询到所有的skuId
        List<Long> ids=skuInfoService.getAllSkuIds();
        //然后添加到布隆中去
        for (Long id : ids) {
            skuFilter.add(id);
        }
    }
}
